<?php

    $rootDir = $_SERVER['DOCUMENT_ROOT'];

    require_once($rootDir.'/classes/db/SQLiteConnector.class.php');   
    require_once($rootDir.'/classes/db/LeproUserDAO.class.php');
    require_once($rootDir.'/classes/db/LeproBJGroupDAO.class.php');
    require_once($rootDir.'/classes/SessionManager.class.php');
    require_once($rootDir.'/classes/LeproBJAppSettings.class.php');
    
    $tUID = urldecode($_GET['uid']);
    
    date_default_timezone_set('Europe/Moscow');

    if (SessionManager::isUserLoggedIn()) {
        $userId = SessionManager::getCurrentUserID();
        if (($userId != null) && ($tUID == $userId) && 
            (preg_match("/^\d*$/", $tUID) > 0) && SessionManager::isUserChecked($tUID)) {
            $connector = SQLiteConnector::getInstance();
            if ($connector->connect()) { // connect to DB
                $userDAO = new LeproUserDAO($connector->getDB());
                $groupDAO = new LeproBJGroupDAO($connector->getDB()); 
                $user = $userDAO->load($userId); // try to load this user from DB
                if ($user != null) {
                    $weekDay = date("w");
                    echo json_encode(array(
                        "groups" => $user->getGroupsIDs(),
                        "lallow" => in_array($weekDay, LeproBJAppSettings::$LALLOWED_DAYS),
                        "total" => $groupDAO->getCount(),
                        "created" => ($groupDAO->userGroupsCount($userId) > 0),
                        "groupscnt" => $user->getGroupsCount(),
                        "curscore" => $user->getGScore() 
                        ));
                } else echo ':(';
            }
        } else echo ':(';
    }

?>
